package com.wish.group05.shoufang.sign;

import com.itextpdf.io.codec.Base64;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.signatures.IExternalSignatureContainer;
import com.itextpdf.signatures.PdfSignatureAppearance;
import junit.framework.TestCase;
import org.bouncycastle.util.io.Streams;
import org.junit.Before;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class DeferredSignPdfAssLryTest extends TestCase {
    public static final String uuid= UUID.randomUUID().toString();
    public static final String FOLDER = "D:\\pic\\";
    public static final String SOURCEFILE = FOLDER + uuid+"_my.pdf";
    public static final String TEMPFILE = FOLDER + uuid+"_empty.pdf";
    public static final String RESULTFILE =  FOLDER + "_signed.pdf";
    public static final String TEMPFILE2 = FOLDER  + "_empty2.pdf";
    public static final String RESULTFILE2 =  FOLDER + "_signed2.pdf";
    public static final String singImg =  FOLDER + "store\\sign.gif";
    public static final String singImg2 =  FOLDER + "store\\sign2.gif";
    String fieldNameA = "SIGNATURE_A";
    String fieldNameB = "SIGNATURE_B";
    @Before
    public void init(){
    }
    @Test
    public void testInitSigner() {
        DeferredSignPdfAssLry signPdfAss = new DeferredSignPdfAssLry();
        Rectangle rectangle = new Rectangle(36, 748, 200, 100);
        SignInfo signInfo = new SignInfo(rectangle,1,"第一次签名","企业签名",fieldNameA,singImg,true);
        //获取空的签名文件
        ExternalEmptySignatureContainer externalSignatureContainer = new ExternalEmptySignatureContainer(signPdfAss.getDicFilter(),signPdfAss.getDicSubfilter(),signPdfAss.getHashAlgorithm());
        //初始化
        try {
            signPdfAss.initSigner(Streams.readAll(new FileInputStream(SOURCEFILE)),TEMPFILE,signInfo,externalSignatureContainer);
        } catch (IOException | GeneralSecurityException e) {
            System.out.println("初始化签名对象异常");
            e.printStackTrace();
        }
        //待签名哈希,发给外部服务器进行签名
        String ToSignHash = Base64.encodeBytes(externalSignatureContainer.getDigestHash()) ;
        String signedHash = null;//cfca签名后的哈希值
        try {
            signedHash = SignService.sign(ToSignHash,signPdfAss.getHashAlgorithm());
        } catch (GeneralSecurityException e) {
            System.out.println("签名异常！");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("签名异常！");
            e.printStackTrace();
        }
        //验签，这一步需要通过外部服务器返回的signedHash，与原文件哈希值externalSignatureContainer.getDigestHash()进行验证

        byte[] cmsInfo = Base64.decode(signedHash);
        try {
            DeferredSignPdfAssLry.embedSignature(TEMPFILE,RESULTFILE,fieldNameA,new EmbdedSignatureContainer(cmsInfo));
        } catch (IOException e) {
            System.out.println("分离签名异常！");
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            System.out.println("分离签名异常！");
            e.printStackTrace();
        }
        //第二次签名
        testInitSignerSec();
    }
    @Test
    public void testInitSignerSec() {
        DeferredSignPdfAssLry signPdfAss = new DeferredSignPdfAssLry();
        Rectangle rectangle = new Rectangle(300, 748, 200, 100);
        SignInfo signInfo = new SignInfo(rectangle,2,"第2次签名","主体签名",fieldNameB,singImg2,true);
        //获取空的签名文件
        ExternalEmptySignatureContainer externalSignatureContainer = new ExternalEmptySignatureContainer(signPdfAss.getDicFilter(),signPdfAss.getDicSubfilter(),signPdfAss.getHashAlgorithm());
        //初始化
        try {
            signPdfAss.initSigner(Streams.readAll(new FileInputStream(RESULTFILE)),TEMPFILE2,signInfo,externalSignatureContainer);
        } catch (IOException | GeneralSecurityException e) {
            System.out.println("初始化签名对象异常");
            e.printStackTrace();
        }
        //待签名哈希,发给cfca进行签名
        String ToSignHash = Base64.encodeBytes(externalSignatureContainer.getDigestHash()) ;
        String signedHash = null;//cfca签名后的哈希值
        try {
            signedHash = SignService.sign(ToSignHash,signPdfAss.getHashAlgorithm());
        } catch (GeneralSecurityException e) {
            System.out.println("签名异常！");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("签名异常！");
            e.printStackTrace();
        }
        byte[] cmsInfo = Base64.decode(signedHash);
        try {
            DeferredSignPdfAssLry.embedSignature(TEMPFILE2,RESULTFILE2,fieldNameB,new EmbdedSignatureContainer(cmsInfo));
        } catch (IOException e) {
            System.out.println("分离签名异常！");
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            System.out.println("分离签名异常！");
            e.printStackTrace();
        }
    }
}
